package srx.open.com.utils;

import android.content.Context;
import android.os.Vibrator;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

import de.greenrobot.event.EventBus;
import srx.open.com.bean.SrxApplication;

public class BaiDuLbsHelper {
    private static final String TAG = "BaiDuLbsHelper";
    public static final String BAIDU_BD09LL = "bd09ll"; // 百度加密经纬度坐标.

    private LocationMode locationMode = LocationMode.Hight_Accuracy; // 高精度.
    private int span = 1000 * 60 * 10; // 1000(1秒) * 60 * 1 = 一分钟.
    public LocationClient mLocationClient;
    public Vibrator mVibrator;
    private MyLocationListener mMyLocationListener;
    private SrxApplication mSrxApplication;

    public BaiDuLbsHelper(Context context) {
        Log.i(TAG, "BaiDuLbsHelper start===");
        /* 百度定位SDK初始化 */
        mLocationClient = new LocationClient(context);
        mMyLocationListener = new MyLocationListener();
        mLocationClient.registerLocationListener(mMyLocationListener);
        mSrxApplication = ((SrxApplication)context);
        initLocation();
        // mVibrator
        // =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        // // 震动.
        Log.i(TAG, "BaiDuLbsHelper end===");
    }

    /*
     * 初始化百度定位-地理位置.
     */
    private void initLocation() {
        try {
            Log.i(TAG, "initLocation start===");
            LocationClientOption option = new LocationClientOption();
            option.setLocationMode(locationMode);// 设置定位模式
            option.setCoorType(BAIDU_BD09LL);// 返回的定位结果是百度经纬度，默认值gcj02
            option.setScanSpan(span);// 设置发起定位请求的间隔时间为5000ms
            option.setIsNeedAddress(true); // 反地理位置[可以获取地址]
            mLocationClient.setLocOption(option);
            Log.i(TAG, "initLocation end===");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 实现实位回调监听
     */
    public class MyLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
//			 Receive Location
            StringBuffer sb = new StringBuffer(256);
            sb.append("time : ");
            sb.append(location.getTime());
            sb.append("\nerror code : ");
            sb.append(location.getLocType());
            sb.append("\nlatitude : ");
            sb.append(location.getLatitude());
            sb.append("\nlontitude : ");
            sb.append(location.getLongitude());
            sb.append("\nradius : ");
            sb.append(location.getRadius());
            if (location.getLocType() == BDLocation.TypeGpsLocation) {
                sb.append("\nspeed : ");
                sb.append(location.getSpeed());
                sb.append("\nsatellite : ");
                sb.append(location.getSatelliteNumber());
                sb.append("\ndirection : ");
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                sb.append(location.getDirection());
            } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
                sb.append("\naddr : ");
                sb.append(location.getAddrStr());
                // 运营商信息
                sb.append("\noperationers : ");
                sb.append(location.getOperators());
            }
            //  这里的API要用于首页.
            sb.append("\n Province: " + location.getProvince()); // 获取省份信息.
            sb.append("\n City: " + location.getCity()); //  获取城市信息.
            sb.append("\n District: " + location.getDistrict()); // 获取区县信息.
            //
            Log.i(TAG, "百度定位:" + sb.toString());
            EventBus.getDefault().post(location); // 发送定位更新信号.
        }
    }
}
